import {request} from '@/plugins/request.js'

// 获取所有文章
export const getArticles = params => {
    return request({
        method: 'GET',
        url: '/api/articles',
        params  // params是个查询参数对象，是个可选值 形式为 {key: params.xxx}
    })
}


// 获取已关注用户的文章
export const getFeedArticles = params => {
    return request({
        method: 'GET',
        url: '/api/articles/feed',
        // headers: {
        //     // token格式 : Authorization: Token jwt.token.here
        //     Authorization: `Token "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6MTQ1Mzk2LCJ1c2VybmFtZSI6ImplbGx5amsiLCJleHAiOjE2MTk2ODcwOTV9.Cs751NsTImbwTmOawF-xWcYnkRp_Hn5Q1jnX_kiClzo"`
        // },
        params
    })
}


// 添加点赞
export const addFavorite = slug => {
    return request({
        method:'POST',
        url:`/api/articles/${slug}/favorite`
    })
}


// 取消点赞
export const deleteFavorite = slug => {
    return request({
        method:'DELETE',
        url:`/api/articles/${slug}/favorite`
    })
}

// 获取文章详情
export const getArticle = slug => {
    return request({
        method:'GET',
        url:`/api/articles/${slug}`
    })
}

// 获取文章评论
export const getComments = slug => {
    return request({
        method:'GET',
        url:`/api/articles/${slug}/comments`
    })
}


// 创建文章
export const newArticle = data => {
    return request({
      method: 'POST',
      url: 'api/articles',
      data
    })
  }
  
  // 更新文章
  export const updateArticle = (data, slug) => {
    return request({
      method: 'PUT',
      url: `api/articles/${ slug }`,
      data
    })
  }